package com.google.code.tretris.rendering;

import javax.microedition.khronos.opengles.GL10;

import android.opengl.GLU;
import android.util.Log;

public class Camera implements Renderable {
	private static final String LOG_TAG = Camera.class.getName();
	public static final float CLIP_PLANE_NEAR = 2.0f;
	public static final float CLIP_PLANE_FAR = 200.0f;
	private float x, y, z, rx, ry, rz;

	public Camera() {
		super();
	}

	public void setPosition(final float x, final float y, final float z) {
		this.x = x;
		this.y = y;
		this.z = z;
	}

	public void setXRotation(final float rx) {
		this.rx = rx;
	}

	public void setYRotation(final float ry) {
		this.ry = ry;
	}

	public void setZRotation(final float rz) {
		this.rz = rz;
	}

	public void setupProjection(final GL10 gl, int width, int height) {
		Log.d(LOG_TAG, "Set up perspective projection: 60 fovy " + width + " "
				+ height);
		gl.glMatrixMode(GL10.GL_PROJECTION);
		gl.glLoadIdentity();
		GLU.gluPerspective(gl, 60, (float) width / (float) height,
				CLIP_PLANE_NEAR, CLIP_PLANE_FAR);
	}

	/**
	 * Position the camera. Does not set matrix mode but assumes MODELVIEW
	 * 
	 * @param gl
	 */
	public void render(final GL10 gl) {
		gl.glRotatef(-rz, 0, 0, 1);
		gl.glRotatef(-ry, 0, 1, 0);
		gl.glRotatef(-rx, 1, 0, 0);
		gl.glTranslatef(-x, -y, -z);
	}
}
